package com.example.education.service.impl;

import com.example.education.exception.ParamException;
import com.example.education.dao.CollectMapper;
import com.example.education.dao.CourseMapper;
import com.example.education.model.Collect;
import com.example.education.model.Course;
import com.example.education.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;
    @Autowired
    CourseMapper courseMapper;

    @Override
    public boolean insertCollect(Collect collect) throws ParamException {
        if(collect==null){
            throw new ParamException("参数为空");
        }else if(collect.getCourseId()==null){
            throw new ParamException("CourseId为空");
        }else if(collect.getUserId()==null){
            throw new ParamException("getUserId为空");
        }
        collect.setCreatTime(new Date());
        Integer result =  collectMapper.insertCollect(collect);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean removeCollect(Integer collectId) throws ParamException {
        if(collectId==null){
            throw new ParamException("参数为空");
        }
        Integer result =  collectMapper.removeCollect(collectId);
        if(result>0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Collect getCollectById(Integer collectId) throws ParamException {
        if(collectId==null){
            throw new ParamException("参数为空");
        }
        return collectMapper.getCollectById(collectId);
    }

    @Override
    public List<Course> getCollectByUserId(Integer userId) throws ParamException {
        if(userId==null){
            throw new ParamException("参数为空");
        }
        List<Collect> collects =  collectMapper.getCollectByUserId(userId);
        List<Integer> courseIds = new ArrayList<>();
        for(Collect collect:collects){
            courseIds.add(collect.getCourseId());
        }
        return courseMapper.getCourseByIds(courseIds);
    }
}
